﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_testQuestionManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string course_id = "";
        string test_question_name = "";

        System.Data.DataTable table1 = new System.Data.DataTable();
        int count = 0;//总记录的条数
        try
        {
            //填充数据
            int page = Int32.Parse(Request.QueryString["page"].ToString().Trim());
            int limit = Int32.Parse(Request.QueryString["limit"].ToString().Trim());
            if (Request.QueryString["course_id"] != null)
            {
                course_id = Request.QueryString["course_id"].ToString().Trim();
            }
            if (Request.QueryString["test_question_name"] != null)
            {
                test_question_name = Request.QueryString["test_question_name"].ToString().Trim();
            }
            
            string sqlGetDataCount = "select count(*) from tb_test_questions ";
            List<string> wheres = new List<string>();

            if (course_id != "")
            {
                wheres.Add("course_id like '%" + course_id + "%'");
            }
            if (test_question_name != "")
            {
                wheres.Add("test_question_name like '%" + test_question_name + "%'");
            }
            string wh = "";
            if (wheres.Count > 0)
            {
                wh = string.Join(" and ", wheres.ToArray());
                sqlGetDataCount = sqlGetDataCount + " where " + wh;
            }
            count = Int32.Parse(SqlServer.ExecuteScalar(sqlGetDataCount));
            int page_temp = page;
            int limit_temp = limit;
            if (count < page * limit)
            {
                limit = count - (page - 1) * limit;
            }
            page = (page_temp - 1) * limit_temp;
            //string sql = "select top " + limit + " a.test_question_id ,a.test_question_name ,c.college_name, b.major_name,a.test_question_sex,a.test_question_tel,a.test_question_id_card from tb_test_question as a "
            //+ " inner join tb_major as b on a.major_id=b.major_id "
            //+ "inner join tb_college as c on b.college_id = c.college_id";
            string sql = "select top " + limit + "test_question_id" +
                                    "      ,test_question_name" +
                                    "      ,test_score" +
                                    "      ,test_difficulty_level" +
                                    "      ,course_id" +
                                    "      ,radio_questions_sum" +
                                    "      ,mutil_questions_sum" +
                                    "      ,judge_questions_sum" +
                                    "      ,answer_questions_sum" +
                                    "  FROM dbo.tb_test_questions ";
            if (wheres.Count > 0)
            {
                sql = sql + " where " + wh + " and test_question_id not in (select top " + page + "  test_question_id from tb_test_questions " + " where " + wh + ")";
            }
            else
            {
                sql = sql + " where test_question_id not in (select top " + page + "  test_question_id from tb_test_questions)";
            }
            table1 = SqlServer.GetDataTable(sql);
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('有错误')</script>");
            Response.End();
        }
        string a = Json.ToJson(table1, count);
        Response.Write(Json.ToJson(table1, count));
        Response.End();
    }
}